package com.linkturing.swing;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import com.alibaba.fastjson.JSON;
import com.linkturing.serial.AddWeightBean;
import com.linkturing.util.Constants;
import com.linkturing.util.JedisUtil;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;

public class AddWeightSettingNew extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	JFrame frame = null;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					AddWeightSettingNew frame = new AddWeightSettingNew();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AddWeightSettingNew() {
		
		frame = this;
		
		this.addWindowFocusListener(new WindowFocusListener(){
			@Override
			public void windowGainedFocus(WindowEvent e){}
			@Override
			public void windowLostFocus(WindowEvent e){
			e.getWindow().toFront();
			}
			});

		//新增
		setTitle("\u65B0\u589E");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(340, 220, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(10, 10, 414, 241);
		contentPane.add(panel);
		panel.setLayout(null);

		//起始重量：
		JLabel lblNewLabel = new JLabel("\u8D77\u59CB\u91CD\u91CF\uFF1A");
		lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel.setBounds(47, 40, 72, 31);
		panel.add(lblNewLabel);

		//结束重量：
		JLabel lblNewLabel_1 = new JLabel("\u7ED3\u675F\u91CD\u91CF\uFF1A");
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel_1.setBounds(47, 81, 72, 31);
		panel.add(lblNewLabel_1);

		//加重：
		JLabel lblNewLabel_1_1 = new JLabel("\u52A0\u91CD\uFF1A");
		lblNewLabel_1_1.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel_1_1.setBounds(47, 122, 72, 31);
		panel.add(lblNewLabel_1_1);
		
		textField = new JTextField();
		textField.setBounds(146, 40, 116, 31);
		panel.add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(146, 81, 116, 31);
		panel.add(textField_1);
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		textField_2.setBounds(146, 127, 116, 31);
		panel.add(textField_2);

		//取消
		JButton btnNewButton = new JButton("\u53D6\u6D88");
		btnNewButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				frame.setVisible(false);
				/*
				 * textField.setText(""); textField_1.setText(""); textField_2.setText("");
				 */
			}
		});
		btnNewButton.setBounds(95, 179, 93, 31);
		panel.add(btnNewButton);

		//确定
		JButton btnNewButton_1 = new JButton("\u786E\u5B9A");
		btnNewButton_1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String start = textField.getText();
				String end = textField_1.getText();
				String weight = textField_2.getText();
				
				if(StrUtil.isBlank(start)) {
					JOptionPane.showMessageDialog(frame, "請輸入起始重量");
					return;
				}
				if(StrUtil.isBlank(end)) {
					JOptionPane.showMessageDialog(frame, "請輸入结束重量");
					return;
				}
				if(!Validator.isNumber(start)) {
					JOptionPane.showMessageDialog(frame, "起始重量是數字");
					return;
				}
				
				if(StrUtil.isBlank(end)) {
					JOptionPane.showMessageDialog(frame, "起始重量長度");
					return;
				}
				if(StrUtil.isBlank(weight)) {
					JOptionPane.showMessageDialog(frame, "請輸入加重");
					return;
				}
				if(!Validator.isNumber(weight)) {
					JOptionPane.showMessageDialog(frame, "加重必须是數字");
					return;
				}
				
				AddWeightBean add = new AddWeightBean();
				add.setStart(start);
				add.setEnd(end);
				add.setWeightIncrease(weight);
				JedisUtil.lpush(Constants.addweightSetting,JSON.toJSONString(add));
				frame.dispose();
				AddWeightSetting.dataTable.reloadTable(Constants.addweightSetting);
			}
		});
		btnNewButton_1.setBounds(218, 179, 93, 31);
		panel.add(btnNewButton_1);
		
	}

}
